#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import oss2
import os
import time
# sys.path.append('/opt/work/caffe/python')
sys.path.insert(0, '.')


############################################################################

list_sub_dir_and_file = True

START_TIME = '20200906_000000'
STOP_TIME = ''

file_path = r'./server_video_list.txt'

prefix_list = [
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa1d4096600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a5444345203bccb439fb500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjyeq/150100414a54443452064d2c43e13600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjyeq/150100414a54443452064d2742c73600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjysq/150100414a54443452064d27432e3600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjysq/150100414a54443452064d2c43ed3600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/dqjd/xgly/150100414a54443452067fa6d45c6600/video', START_TIME.split('_')[0]),

    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a5444345203bcd04287b500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa1d4cd6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d4b06600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/tsh/150100414a5444345203bccb439eb500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/tsh/150100414a54443452064d2743243600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/tsh/150100414a54443452067fa6d45f6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/myxf/150100414a54443452067fa6d4a36600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/ycjy/150100414a54443452064d2742c43600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjyeq/150100414a54443452064d2c43e43600/video', START_TIME.split('_')[0]),

    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d4586600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452064d2742ee3600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d44b6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/dqjd/xgly/150100414a54443452067fa1d4c16600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/dqjd/xgly/150100414a54443452067fa6d4a86600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/dqjd/xgly/150100414a5444345203bcd0428ab500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/dqjd/xgly/150100414a54443452067fa6d4ad6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/dqjd/xgly/150100414a54443452067fa6d4ab6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/ycjy/150100414a54443452064d2742d03600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjyeq/150100414a54443452064d27433e3600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjysq/150100414a54443452064d2743233600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/dxssjysq/150100414a54443452064d2742e33600/video', START_TIME.split('_')[0]),

    os.path.join('gd/zs/dqjd/xgly/150100414a54443452067fa6d4bc6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/dqjd/xgly/150100414a5444345203bcd04294b500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/tsh/150100414a54443452064d2742ca3600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/ycjy/150100414a5444345203bcd0428cb500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/ycjy/150100414a54443452064d2743283600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/ycjy/150100414a54443452067fa6d45b6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/myj/150100414a5444345203bcd04293b500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/myj/150100414a54443452067fa1d40a6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d4be6600/video', START_TIME.split('_')[0]),

    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa1d4c56600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a5444345203bcd04287b500/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d4556600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/yxxhjt/150100414a54443452067fa1d40e6600/video', START_TIME.split('_')[0]),
    os.path.join('gd/zs/sqqjd/myj/150100414a54443452064d2743383600/video', START_TIME.split('_')[0]),
]


if list_sub_dir_and_file:
    delimiter = ''
else:
    delimiter = '/'

# 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth('LTAIxABOHnLXUh8P', 'gUaZTagYxFzh6k6Iu7hk0HvuTEOSlZ')
# Endpoint以杭州为例，其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-shenzhen.aliyuncs.com', 'erised-screenshots')

# objects = bucket.list_objects(delimiter='/', max_keys=1000)

if START_TIME == '':
    start_time = None
else:
    start_time = time.strptime(START_TIME, '%Y%m%d_%H%M%S')
    start_time = time.mktime(start_time) - 3600*8
if STOP_TIME == '':
    stop_time = None
else:
    stop_time = time.strptime(STOP_TIME, '%Y%m%d_%H%M%S')
    stop_time = time.mktime(stop_time) - 3600*8

dir_name = os.path.dirname(file_path)
if not os.path.exists(dir_name):
    os.makedirs(dir_name)

with open(file_path, 'w') as file:
    file_count = 0
    sum_size = 0
    print_count = 0
    max_last_modified = 0
    last_file_name = ''
    for prefix_str in prefix_list:
        is_truncated = True
        next_marker = ''
        while(is_truncated):
            result = bucket.list_objects(prefix=prefix_str, delimiter=delimiter, marker=next_marker, max_keys=1000)
            is_truncated = result.is_truncated
            next_marker = result.next_marker
            oss_objects = result.object_list
            for oss_object in oss_objects:
                last_modified = oss_object.last_modified
                if (start_time is not None) and (last_modified < start_time):
                    continue
                if (stop_time is not None) and (last_modified > stop_time):
                    continue
                file_size = oss_object.size
                sum_size += file_size
                file_count += 1
                file_name = os.path.normpath(oss_object.key)
                file.write('{},{},{}\n'.format(file_name, last_modified, file_size))
                if max_last_modified < last_modified:
                    max_last_modified = last_modified
                    last_file_name = file_name
                print_count += 1
                if print_count == 100:
                    print_count = 0
                    last_time_str = time.strftime("%Y%m%d_%H%M%S", time.localtime(max_last_modified + 3600 * 8))
                    print('Unload File Count:', file_count, ', Size:', sum_size / (1024 * 1024 * 1024), 'GB, Last Time:', last_time_str, last_file_name)
    last_time_str = time.strftime("%Y%m%d_%H%M%S", time.localtime(max_last_modified + 3600 * 8))
    print('Unload File Count:', file_count, ', Size:', sum_size / (1024 * 1024 * 1024), 'GB, Last Time:', last_time_str, last_file_name)
print('Finish!')

# 设置Delimiter参数为正斜线（/）。
# for obj in oss2.ObjectIterator(bucket, delimiter='/', max_keys=1000):
#     # 通过is_prefix方法判断obj是否为文件夹。
#     if obj.is_prefix():  # 文件夹
#         print('directory: ' + obj.key)
#     else:                # 文件
#         print('file: ' + obj.key)

###############################################################################

